package com.hjimi.ski.presenter;

import android.util.Log;

import com.hjimi.ski.bean.SyncImageListBean;
import com.hjimi.ski.callback.BaseCallback;
import com.hjimi.ski.config.Constant;
import com.hjimi.ski.config.UrlConfig;
import com.hjimi.ski.iView.SyncImageListIView;
import com.hjimi.ski.model.base.DataModel;
import com.hjimi.ski.model.base.ModelToken;
import com.hjimi.ski.utils.JsonParseUtil;
import com.hjimi.ski.utils.SpUtil;
import com.hjimi.ski.utils.ToastUtils;
import com.hjimi.ski.view.activity.NewImageActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 * Created By 孙岩
 * on 2023/3/30
 */
public class SyncImageListPresenter extends BasePresenter<SyncImageListIView>{
    @SuppressWarnings("unchecked")
    public void getSyncImageList(String deviceId){
        if(isViewAttached()){
            String url = UrlConfig.getSyncImageList + deviceId;
            DataModel.request(ModelToken.GET_REQUEST)
                    .url(url)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            //2023-10-11 sunyan
                            Log.d("sunyan","yyyyyyyyyyyyyyyyyyyyyyyyyy"+data);
                            SpUtil.getInstance(getView().getContext()).putString(Constant.IMAGE_JSON,data);
                            try {
                                JSONObject obj = new JSONObject(data);
                                if(obj.getInt("code")==200){
                                    JSONArray array = new JSONArray(obj.getString("data"));
                                    List<SyncImageListBean.DataBean> list = new ArrayList<>();

                                    for(int i=0;i<array.length();i++){
                                        JSONObject object = new JSONObject(array.get(i).toString());
                                        JSONObject bgmConfigObj = new JSONObject(object.getString("bgm_config"));
                                        SyncImageListBean.DataBean.BgmConfigBean bgmConfigBean = new SyncImageListBean.DataBean.BgmConfigBean(
                                                JsonParseUtil.getBoolean(bgmConfigObj, "is_open"),
                                                JsonParseUtil.getInt(bgmConfigObj, "bgm_id"),
                                                JsonParseUtil.getString(bgmConfigObj, "bgm_url"),
                                                JsonParseUtil.getBoolean(bgmConfigObj, "is_loop")
                                        );
                                        JSONObject cardConfigObj = new JSONObject(object.getString("card_config"));
                                        SyncImageListBean.DataBean.CardConfigBean cardConfigBean = new SyncImageListBean.DataBean.CardConfigBean(
                                                JsonParseUtil.getBoolean(cardConfigObj, "is_open"),
                                                JsonParseUtil.getString(cardConfigObj, "title"),
                                                JsonParseUtil.getString(cardConfigObj, "desc"),
                                                JsonParseUtil.getString(cardConfigObj, "author"),
                                                JsonParseUtil.getString(cardConfigObj, "period"),
                                                JsonParseUtil.getBoolean(cardConfigObj, "is_narration"),
                                                JsonParseUtil.getBoolean(cardConfigObj, "is_qr"),
                                                JsonParseUtil.getString(cardConfigObj, "qr_url"),
                                                JsonParseUtil.getInt(cardConfigObj, "carousel_seconds")
                                        );
                                        SyncImageListBean.DataBean bean = new SyncImageListBean.DataBean(
                                                JsonParseUtil.getInt(object,"id"),
                                                JsonParseUtil.getStr(object,"name"),
                                                JsonParseUtil.getStr(object,"imageUrl"),
                                                JsonParseUtil.getStr(object,"thumbUrl"),
                                                JsonParseUtil.getStr(object,"qrUrl"),
                                                JsonParseUtil.getInt(object,"artwork_id"),
                                                bgmConfigBean,
                                                cardConfigBean,
                                                JsonParseUtil.getBoolean(object,"is_default")
                                        );
                                        list.add(bean);
                                    }
                                    getView().showSyncImage(list);
                                }else{
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                            getView().hideLoading();
                            Log.d("sunyan","2222222222222222222222222222"+msg);
                        }

                        @Override
                        public void onComplete() {
                            getView().hideLoading();
                        }

                        @Override
                        public void onBefore() {
                            getView().showLoading();
                        }
                    });
        }
    }
}
